﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.Paint.DisplayCoverage.CUiPaintDisplayCoverageUtil
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;


namespace DnProcessSimulateCommands.Paint.DisplayCoverage;

public class CUiPaintDisplayCoverageUtil
{
  private const string REGISTRY_CONSEQUENT_TIME_INTERVAL = "TimeIntervalEpsilon_msec";
  private const string REGISTRY_TURN_DEGREE = "TurnAngle_degree";
  private const string REGISTRY_COVERAGE_COLOR_MAP = "CoverageColorMap";
  private const string REGISTRY_DISPLAY_COVERAGE_MODE = "DisplayCoverageMode";
  private const string REGISTRY_DISPLAY_BRUSH = "DisplayBrush";
  private const string REGISTRY_DISPLAY_THICKNESS_VALUE = "DisplayThicknessValue";
  private const string REGISTRY_IGNORE_HIDDEN_SURFACES = "IgnoreHiddenSurfaces";
  private const string REGISTRY_THICKNESS_SCALE_NAME = "ThicknessScaleName";
  private static ITxCommand m_command;
  private static TxCommandPrivateDataStorage m_registryManager;

  public static void InitRegistryManager(ITxCommand command)
  {
    CUiPaintDisplayCoverageUtil.m_command = command;
    CUiPaintDisplayCoverageUtil.m_registryManager = new TxCommandPrivateDataStorage(CUiPaintDisplayCoverageUtil.m_command);
  }

  public static int GetIntValueFromRegistry(string valueName, int defaultValue)
  {
    int valueFromRegistry = defaultValue;
    int? nullable = CUiPaintDisplayCoverageUtil.LoadRegistryIntValue(valueName);
    if (nullable.HasValue)
      valueFromRegistry = nullable.Value;
    return valueFromRegistry;
  }

  public static void SetIntValueToRegistry(string valueName, int theValue)
  {
    CUiPaintDisplayCoverageUtil.m_registryManager.SetIntValue(valueName, theValue);
  }

  private static int? LoadRegistryIntValue(string valueName)
  {
    int? nullable = new int?();
    int num;
    if (CUiPaintDisplayCoverageUtil.m_registryManager != null && CUiPaintDisplayCoverageUtil.m_registryManager.TryGetIntValue(valueName, ref num))
      nullable = new int?(num);
    return nullable;
  }

  public static Color GetColorValueFromRegistry(string valueName, Color defaultValue)
  {
    Color valueFromRegistry = defaultValue;
    int? nullable = CUiPaintDisplayCoverageUtil.LoadRegistryIntValue(valueName);
    if (nullable.HasValue)
      valueFromRegistry = Color.FromArgb(nullable.Value);
    return valueFromRegistry;
  }

  public static void SetColorValueToRegistry(string valueName, Color theValue)
  {
    CUiPaintDisplayCoverageUtil.m_registryManager.SetIntValue(valueName, theValue.ToArgb());
  }

  private static bool IntToBool(int x) => x == 1;

  private static int BoolToInt(bool val) => val ? 1 : 0;

  public static bool GetBoolValueFromRegistry(string valueName, bool defaultValue)
  {
    bool valueFromRegistry = defaultValue;
    int? nullable = CUiPaintDisplayCoverageUtil.LoadRegistryIntValue(valueName);
    if (nullable.HasValue)
      valueFromRegistry = CUiPaintDisplayCoverageUtil.IntToBool(nullable.Value);
    return valueFromRegistry;
  }

  public static void SetBoolValueToRegistry(string valueName, bool theValue)
  {
    CUiPaintDisplayCoverageUtil.m_registryManager.SetIntValue(valueName, CUiPaintDisplayCoverageUtil.BoolToInt(theValue));
  }

  private static string LoadRegistryStringValue(string valueName)
  {
    string str = (string) null;
    if (CUiPaintDisplayCoverageUtil.m_registryManager != null)
      CUiPaintDisplayCoverageUtil.m_registryManager.TryGetStringValue(valueName, ref str);
    return str;
  }

  public static void SetStringValueToRegistry(string valueName, string theValue)
  {
    CUiPaintDisplayCoverageUtil.m_registryManager.SetStringValue(valueName, theValue);
  }

  private static double StringToDouble(string str)
  {
    str = str.Replace(".", ",");
    return Convert.ToDouble(str, (IFormatProvider) new NumberFormatInfo()
    {
      NumberDecimalSeparator = ",",
      NumberGroupSeparator = "."
    });
  }

  public static double GetDoubleValueFromRegistry(string valueName, double defaultValue)
  {
    double valueFromRegistry = defaultValue;
    string str = CUiPaintDisplayCoverageUtil.LoadRegistryStringValue(valueName);
    if (str != null)
      valueFromRegistry = CUiPaintDisplayCoverageUtil.StringToDouble(str);
    return valueFromRegistry;
  }

  public static void SetDoubleValueToRegistry(string valueName, double theValue)
  {
    CUiPaintDisplayCoverageUtil.m_registryManager.SetStringValue(valueName, Convert.ToString(theValue));
  }

  public static void LoadOverlapValuesFromRegistry(
    out double consequentTimeInterval,
    out double turnAngle)
  {
    turnAngle = CUiPaintDisplayCoverageUtil.GetDoubleValueFromRegistry("TurnAngle_degree", 80.0);
    int valueFromRegistry = CUiPaintDisplayCoverageUtil.GetIntValueFromRegistry("TimeIntervalEpsilon_msec", 5);
    consequentTimeInterval = (double) valueFromRegistry / 1000.0;
  }

  public static List<TxColor> ConvertToTxColor(List<Color> colorList)
  {
    List<TxColor> txColor1 = new List<TxColor>();
    for (int index = 0; index < colorList.Count; ++index)
    {
      Color color = colorList[index];
      TxColor txColor2 = new TxColor(color.R, color.G, color.B);
      txColor1.Insert(index, txColor2);
    }
    return txColor1;
  }

  public static bool LoadIgnoreHiddenSurfacesFromRegistry()
  {
    return CUiPaintDisplayCoverageUtil.GetBoolValueFromRegistry("IgnoreHiddenSurfaces", false);
  }

  public static void StoreIgnoreHiddenSurfacesToRegistry(bool mode)
  {
    CUiPaintDisplayCoverageUtil.SetBoolValueToRegistry("IgnoreHiddenSurfaces", mode);
  }

  public static bool LoadDisplayCoverageModeFromRegistry()
  {
    return CUiPaintDisplayCoverageUtil.GetBoolValueFromRegistry("DisplayCoverageMode", true);
  }

  public static void StoreDisplayCoverageModeToRegistry(bool mode)
  {
    CUiPaintDisplayCoverageUtil.SetBoolValueToRegistry("DisplayCoverageMode", mode);
  }

  public static bool LoadDisplayBrushFromRegistry()
  {
    return CUiPaintDisplayCoverageUtil.GetBoolValueFromRegistry("DisplayBrush", true);
  }

  public static void StoreDisplayBrushToRegistry(bool mode)
  {
    CUiPaintDisplayCoverageUtil.SetBoolValueToRegistry("DisplayBrush", mode);
  }

  public static bool LoadDisplayThicknessValueFromRegistry()
  {
    return CUiPaintDisplayCoverageUtil.GetBoolValueFromRegistry("DisplayThicknessValue", true);
  }

  public static void StoreDisplayThicknessValueToRegistry(bool mode)
  {
    CUiPaintDisplayCoverageUtil.SetBoolValueToRegistry("DisplayThicknessValue", mode);
  }

  public static List<Color> LoadColorListFromRegistry()
  {
    List<Color> colorList = new List<Color>();
    colorList.Insert(0, Color.Blue);
    colorList.Insert(1, Color.CornflowerBlue);
    colorList.Insert(2, Color.Cyan);
    colorList.Insert(3, Color.LawnGreen);
    colorList.Insert(4, Color.Green);
    colorList.Insert(5, Color.Yellow);
    colorList.Insert(6, Color.DarkOrange);
    colorList.Insert(7, Color.Red);
    colorList.Insert(8, Color.SaddleBrown);
    for (int index = 0; index < colorList.Count; ++index)
    {
      string valueName = "CoverageColorMap" + index.ToString();
      Color defaultValue = colorList[index];
      colorList[index] = CUiPaintDisplayCoverageUtil.GetColorValueFromRegistry(valueName, defaultValue);
    }
    return colorList;
  }

  public static void StoreColorListToRegistry(List<TxColor> colorList)
  {
    for (int index = 0; index < colorList.Count; ++index)
      CUiPaintDisplayCoverageUtil.SetColorValueToRegistry("CoverageColorMap" + index.ToString(), colorList[index].Color);
  }

  public static string LoadThicknessScaleNameFromRegistry()
  {
    return CUiPaintDisplayCoverageUtil.LoadRegistryStringValue("ThicknessScaleName");
  }

  public static void StoreThicknessScaleNameToRegistry(string currentScaleName)
  {
    CUiPaintDisplayCoverageUtil.SetStringValueToRegistry("ThicknessScaleName", currentScaleName);
  }

  public static bool IsObjectVisible(ITxObject obj)
  {
    bool flag = false;
    if (obj is ITxDisplayableObject)
    {
      TxDisplayableObjectVisibility visibility = (obj as ITxDisplayableObject).Visibility;
      if (visibility == null || visibility == 1)
        flag = true;
    }
    return flag;
  }

  public static TxObjectList FindVisibleObjects(TxObjectList objList)
  {
    TxObjectList visibleObjects = new TxObjectList();
    foreach (ITxObject itxObject in (Collection<ITxObject>) objList)
    {
      if (CUiPaintDisplayCoverageUtil.IsObjectVisible(itxObject))
        ((Collection<ITxObject>) visibleObjects).Add(itxObject);
    }
    return visibleObjects;
  }

  public static TxObjectList FindPaintBrushes(ITxObject selectedBrush)
  {
    TxObjectList paintBrushes = new TxObjectList();
    if (selectedBrush is ITxObjectCollection)
      paintBrushes = CUiPaintDisplayCoverageUtil.FindVisibleObjects((selectedBrush as ITxObjectCollection).GetDirectDescendants((ITxTypeFilter) new TxTypeFilter(typeof (ITx2Or3DimensionalGeometry))));
    return paintBrushes;
  }

  public static TxObjectList FindPaintBrushesTipFrames(ITxObject selectedBrush)
  {
    TxObjectList brushesTipFrames = new TxObjectList();
    if (selectedBrush is ITxObjectCollection)
      brushesTipFrames = CUiPaintDisplayCoverageUtil.FindVisibleObjects((selectedBrush as ITxObjectCollection).GetDirectDescendants((ITxTypeFilter) new TxTypeFilter(typeof (TxFrame))));
    return brushesTipFrames;
  }

  public static bool FindBlankMeshesWithCoverage()
  {
    bool meshesWithCoverage = false;
    foreach (TxMeshComponent allDescendant in (Collection<ITxObject>) TxApplication.ActiveDocument.PhysicalRoot.GetAllDescendants((ITxTypeFilter) new TxTypeFilter(typeof (TxMeshComponent))))
    {
      if (!CUiPaintDisplayCoverageUtil.IsObjectVisible((ITxObject) TxMeshComponentEx.GetPart(allDescendant)) && TxMeshComponentEx.HasCoverage(allDescendant))
        meshesWithCoverage = ((meshesWithCoverage ? 1 : 0) | 1) != 0;
    }
    return meshesWithCoverage;
  }

  public static TxObjectList<Tx2Or3DimensionalGeometry> GetMeshEntitiesOfDisplayedParts()
  {
    TxObjectList<Tx2Or3DimensionalGeometry> ofDisplayedParts = new TxObjectList<Tx2Or3DimensionalGeometry>();
    foreach (TxMeshComponent studyMesh in (Collection<ITxObject>) TxApplicationEx.PaintManager().StudyMeshes)
    {
      if (CUiPaintDisplayCoverageUtil.IsObjectVisible((ITxObject) TxMeshComponentEx.GetPart(studyMesh)))
      {
        foreach (Tx2Or3DimensionalGeometry allDescendant in (Collection<ITxObject>) ((ITxObjectCollection) studyMesh).GetAllDescendants((ITxTypeFilter) new TxTypeFilter(typeof (Tx2Or3DimensionalGeometry))))
          ((Collection<Tx2Or3DimensionalGeometry>) ofDisplayedParts).Add(allDescendant);
      }
    }
    return ofDisplayedParts;
  }

  public static void UpdateMeshVisibility()
  {
    foreach (TxMeshComponent allDescendant in (Collection<ITxObject>) TxApplication.ActiveDocument.PhysicalRoot.GetAllDescendants((ITxTypeFilter) new TxTypeFilter(typeof (TxMeshComponent))))
      TxMeshComponentEx.UpdateVisibility(allDescendant);
  }

  public static void DisplayAllMeshes()
  {
    foreach (TxMeshComponent allDescendant in (Collection<ITxObject>) TxApplication.ActiveDocument.PhysicalRoot.GetAllDescendants((ITxTypeFilter) new TxTypeFilter(typeof (TxMeshComponent))))
    {
      if (CUiPaintDisplayCoverageUtil.IsObjectVisible((ITxObject) TxMeshComponentEx.GetPart(allDescendant)))
        TxMeshComponentEx.Display(allDescendant);
    }
  }

  public static bool ReloadFiles(bool showMsgFlag = false)
  {
    bool flag = true;
    try
    {
      TxPaintThicknessFunctionHyperLoader.The.Load();
      TxApplicationEx.PaintManager().ReloadFiles();
    }
    catch (TxPaintThicknessScaleFileNotFoundException ex)
    {
      flag = false;
      if (showMsgFlag)
      {
        int num = (int) TxMessageBox.Show(PaintDisplayCoverageResource.THICKNESS_SCALE_FILE_NOT_FOUND, PaintDisplayCoverageResource.CMD_NAME, MessageBoxButtons.OK, MessageBoxIcon.Hand);
      }
    }
    catch (TxPaintThicknessScaleFileInvalidException ex)
    {
      flag = false;
      if (showMsgFlag)
      {
        int num = (int) TxMessageBox.Show(PaintDisplayCoverageResource.THICKNESS_SCALE_FILE_INVALID, PaintDisplayCoverageResource.CMD_NAME, MessageBoxButtons.OK, MessageBoxIcon.Hand);
      }
    }
    catch (TxPaintBrushGraphFileNotFoundException ex)
    {
      flag = false;
      if (showMsgFlag)
      {
        int num = (int) TxMessageBox.Show(PaintDisplayCoverageResource.BRUSH_GRAPHS_FILE_NOT_FOUND, PaintDisplayCoverageResource.CMD_NAME, MessageBoxButtons.OK, MessageBoxIcon.Hand);
      }
    }
    catch (TxPaintBrushGraphFileInvalidException ex)
    {
      flag = false;
      if (showMsgFlag)
      {
        int num = (int) TxMessageBox.Show(PaintDisplayCoverageResource.BRUSH_GRAPHS_FILE_INVALID, PaintDisplayCoverageResource.CMD_NAME, MessageBoxButtons.OK, MessageBoxIcon.Hand);
      }
    }
    catch (TxPaintFilesNotOKException ex)
    {
      flag = false;
      if (showMsgFlag)
      {
        int num = (int) TxMessageBox.Show(PaintDisplayCoverageResource.BOTH_FILES_NOT_OK, PaintDisplayCoverageResource.CMD_NAME, MessageBoxButtons.OK, MessageBoxIcon.Hand);
      }
    }
    return flag;
  }

  public static void SetThicknessScaleByName(string scaleName)
  {
    TxApplicationEx.PaintManager().SetThicknessScaleByName(scaleName);
  }

  public static void RedrawMeshesUsingCurrentScale()
  {
    TxApplicationEx.PaintManager().ApplyColors();
    TxApplication.RefreshDisplay();
  }

  public static bool DoFilesExist()
  {
    bool flag = true;
    string installationDirectory = TxApplication.InstallationDirectory;
    string str1 = installationDirectory + "\\bin\\Paint\\ThicknessScale.csv";
    string str2 = installationDirectory + "\\bin\\Paint\\PaintGraph.csv";
    return flag;
  }
}
